<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主体名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入主体名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主体类型" prop="type">
        <el-select v-model="queryParams.type" @change="handleQuery" placeholder="请选择主体类型" clearable>
          <el-option
            v-for="dict in dict.type.business_entity_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主体属性" prop="attr">
        <el-select v-model="queryParams.attr" @change="handleQuery" placeholder="请选择主体属性" clearable style="width: 240px">
          <el-option
            v-for="dict in dict.type.business_entity_attribute"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主体状态" prop="status">
        <!-- 认证状态（0-未认证，1-已认证，2-审核中，3-审核失败） -->
        <el-select v-model="queryParams.status" @change="handleQuery" placeholder="请选择认证状态" clearable style="width: 240px">
          <el-option label="待审核" value="2" />
          <el-option label="审核失败" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主体名称" align="center" prop="name" />
      <el-table-column label="认证账号" align="center" prop="account" />
      <el-table-column label="主体属性" align="center" prop="attr" >
        <template slot-scope="scope">
          <span v-for="obj in dict.type.business_entity_attribute">
            <span v-if="obj.value === scope.row.attr"> {{ obj.label }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="主体类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span v-for="obj in dict.type.business_entity_type">
            <span v-if="obj.value === scope.row.type"> {{ obj.label }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="主体状态" align="center" prop="status" >
        <template slot-scope="scope">
          <!-- 认证状态（0-未认证，1-已认证，2-审核中，3-审核失败） -->
          <el-tag v-if="scope.row.authStatus === '2'" type="success" size="mini" >待审核</el-tag>
          <el-tag v-if="scope.row.authStatus === '3'" type="danger" size="mini" >审核失败</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="认证时间" align="center" prop="authTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.authStatus === '2'" type="text" icon="el-icon-s-order"
            @click="handleAuth(scope.row)" v-hasPermi="['business:auth:operation']" style="margin-left: 10px;">审核</el-button>

          <router-link :to="'/information/details/index/auth/' + scope.row.userId + '/' + scope.row.entityId" v-hasPermi="['business:entity:auth']">
            <el-button size="mini" type="text" icon="el-icon-info" >主体详情</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="审核" :visible.sync="dialogVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="30%">
      <el-form :model="authForm" size="mini" label-width="120px">
        <el-form-item label="审核结果：">
          <el-select v-model="authForm.result" placeholder="请选择…" style="width: 100%;">
            <el-option label="通过" :value="'1'"></el-option>
            <el-option label="不通过" :value="'3'"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="authForm.result === '3' ? '原因：' : '备注：'">
          <el-input v-model="authForm.reasons" type="textarea" resize="none" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入…"> </el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleAuthClose">取 消</el-button>
        <el-button type="primary" @click="doAuth">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import {entityAuthList, authEntity} from "@/api/business/entity";

export default {
  name: "Users",
  dicts: ['business_entity_type', 'business_entity_attribute'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 信息表格数据
      infoList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        attr: null,
        status: null,
      },

      dialogVisible: false,
      authEntityId: '',
      authForm: {
        result: '',
        reasons: '',
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询生产人信息列表 */
    getList() {
      this.loading = true
      entityAuthList(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 审核操作 */
    handleAuth(row) {
      this.authEntityId = row.entityId;
      this.dialogVisible = true;
    },
    handleAuthClose(){
      this.dialogVisible = false;
      this.authEntityId = "";
      this.authForm = {
        result: '',
        reasons: '',
      };
    },
    doAuth(){
      let that = this;
      if(!that.authForm.result) {
        that.$message({
          message: '请选择审核结果！',
          type: 'warning'
        });
        return ;
      }

      // 不通过时，必须写明原因
      if("3" === that.authForm.result && !that.authForm.reasons) {
        that.$message({
          message: '请输入审核不通过的原因！',
          type: 'warning'
        });
        return ;
      }

      that.loading = true;
      authEntity(that.authEntityId, that.authForm).then(res => {
        that.loading = false;
        that.getList()
        that.$modal.msgSuccess("审核成功！");
        that.handleAuthClose();
      })
    }
  }
}
</script>
